package adaptors;

import java.util.ArrayList;

import javax.swing.table.*;

import tools.Tab;


public class QueryTableAdaptor extends AbstractTableModel 
{
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String[] columnNames;
    private Object[][] data;
    private ArrayList<Tab> rawRowData;
    
    public QueryTableAdaptor(String[] columnNames)
    {
    	rawRowData = new ArrayList<Tab>();
    	this.columnNames = columnNames;
    	this.data = QueryTableAdaptor.blankTable();
    }
    
    public void setData(Object[][] data)
    {
    	if( data == null )	{
    		System.err.println("Attempting to add null data object as table");
    	}
    	else	{
//    		System.out.println("Actual table population: " + data.length);
    		this.data = data;
    		fireTableDataChanged();
    	}
    }
    
    public void redrawTable()	{
    	fireTableDataChanged();
    }
    
    public int getColumnCount() 
    {
        return columnNames.length;
    }

    public int getRowCount() 
    {
        return data.length;
    }

    public String getColumnName(int col) 
    {
        return columnNames[col];
    }

    public Object getValueAt(int row, int col) 
    {
        return data[row][col];
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
	public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }

    public void setValueAt(Object value, int row, int col) 
    {
        data[row][col] = value;
        fireTableCellUpdated(row, col);
    }
    
    
    // returns blank table object for when selections may change...
    public static Object[][] blankTable()
    {
    	Object[][] newData = new Object[22][4];
    	for(int x=0; x < newData.length; x++)	{
    		newData[x][0] = " ";
    		newData[x][1] = " ";
    		newData[x][2] = " ";
    		newData[x][3] = " ";
    	}

    	return newData;
    }

	public static Object[][] formatTableInformation(Object[] tabListing) 
	{
		Tab q;
		Object[][] unfilteredData = new Object[tabListing.length][4];
		Object[][] newData = null;		
		int size = tabListing.length;
		
		int rowPos = 0;

		for(int x=0; x < size; x++)	{
			
			q = (Tab)tabListing[x];

			if( q == null )
				continue;	// advance to next iteration
				
			unfilteredData[rowPos][0] = q.getBandName();
			unfilteredData[rowPos][1] = q.getSongName();
			unfilteredData[rowPos][2] = q.getRating() + " (" + q.getNumRated() + ")";
			unfilteredData[rowPos][3] = q.getTabType();
			rowPos++;
			
			newData = new Object[rowPos][4];
			rowPos = 0;
			for(int g=0; g < unfilteredData.length; g++)	{
				if( unfilteredData[g][0] != null )	{
					newData[rowPos][0] = unfilteredData[g][0];
					newData[rowPos][1] = unfilteredData[g][1];
					newData[rowPos][2] = unfilteredData[g][2];
					newData[rowPos][3] = unfilteredData[g][3];
					rowPos++;
				}
			}
		}
		
		return newData;
	}

	public void addRow(Tab q)
	{
		if( rawRowData != null && q != null )	{
			rawRowData.add(q);
			setData(formatTableInformation(rawRowData.toArray()));
			fireTableDataChanged();
		}
	}
	
	public void clearTable()
	{
		rawRowData.clear();
		data = new Object[15][columnNames.length];
	}
	

	public static String strictArrayFlattener(String[] args, int from, int to)
	{
		String flat = "";
		for(int x=from; x < to; x++)
			flat+=args[x];
	
		// remove space from front and return
		return flat;//.substring(1);
		
	}
	

}